// name：精灵名字
// painter: 绘制器，需另外封装
// behaviors: 行为，精灵行为
const Sprite = function (name, painter, behaviors) {
  this.left = 0,
  this.top = 0,
  this.width = 10,
  this.height = 10,
  this.velocityX = 0,
  this.velocityY = 0,
  this.visible = true,
  this.animating = false,
  this.painter = undefined, // object with paint(sprite, context)
  this.behaviors = [],// objects with execute(sprite, context, time)
  this.text = ""; 
       
  if(name) {
      this.name = name;
  }
  if (painter) {
    this.painter = painter;
  }
}

Sprite.prototype = {
  // painter属性是一个指向Painter对象的引用，使用paint(sprite, context)方法来绘制精灵。
  // behaviors属性指向一个对象数组，数组的每个对象都以execute(sprite, context, time)方法来对精灵进行操作
  paint: function (context) {
    if (this.painter.paint !== undefined && this.visible) {
      this.painter.paint(this, context);
    }
  },

  update: function (context, time) {
    for (var i = this.behaviors.length; i > 0; --i) {
      this.behaviors[i - 1].execute(this, context, time);
    }
  },

  setAttr: function (data) {
    for(var key in data){
      this[key] = data[key];
    }
  }
}

export default Sprite;